-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = 'PWM fan'
VERSION = '1.0.0'

-- 引入必要的库文件(lua编写), 内部库不需要require
local sys = require 'sys'
device = require"device"
 --pwm_ID = 5


spi_lcd = spi.deviceSetup(device.spi,device.spiCS,0,0,8,device.spiSpeed,spi.MSB,1,1)
lcd.init("st7735s",{port = "device",pin_dc=device.lcdDC,pin_rst=device.lcdRST,pin_pwr=device.lcdBL,direction = 1,w = 80,h = 160,xoffset = 24,yoffset = 0},spi_lcd)
lcd.invoff()
--local speed = 0
 local bt_add = 0

 local fan_sp = 0

if fdb.kvdb_init("env", "onchip_fdb") then
    log.info("fdb", "kv数据库初始化成功")

   local fan_sp = fdb.kv_get("fan_speed")
    if fan_sp then
        speed = fan_sp
       -- if fan_sp ~= 0 then
       
        --    bt_add = 3
       -- end
       
    else
        speed = 0
   
    end
end

function save_fanspeed(speed)
    if fdb.kvdb_init("env", "onchip_fdb") then
        log.info("fdb", fdb.kv_set("fan_speed", speed))
    end
end
function get_fanspeed()
    if fdb.kvdb_init("env", "onchip_fdb") then
        log.info("fdb", "kv数据库初始化成功")
       local fan_sp = fdb.kv_get("fan_speed")
        if fan_sp then
            speed = fan_sp
           
        else
            speed = 0
          
        end
    end
    -- body
end
--local BTN_PIN = 9 -- 按实际开发板选取
function add_fanspeed()
   
    if speed >= 0 and speed < 100 then
        speed = speed + 10
    else
        speed = 100
    end
    log.info("风扇增加设置风速"..speed)
    save_fanspeed(speed)

    -- body
end

function del_fanspeed()
    if speed > 0 and speed <= 100 then
        speed = speed - 10
       
    else
        speed = 0
    end
    log.info("风扇减少设置风速"..speed)
    save_fanspeed(speed)
    -- body
end


local button_timer_outtime = 10 --按键定时器: 10ms
local button_shake_time = 1     --按键消抖时间: button_shake_time*button_timer_outtime
local button_long_time = 50    --按键长按时间: button_shake_time*button_timer_outtime
local button_wait_time = 200
local button_detect = true
local button_state = false
local button_cont = 0
-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(device.BTN_PIN, 5)
end

button = gpio.setup(device.BTN_PIN, function() 
        if not button_detect then return end
        button_detect = false
        button_state = true
    end, 
    gpio.PULLUP,
    gpio.FALLING)

button_timer = sys.timerLoopStart(function()
    if button_state then
        if button() == 0 then
            button_cont = button_cont + 1
          local bt_long = button_cont%button_long_time
            if button_cont > button_wait_time and bt_long == 0 then
                print("long pass")
                if fan_sp == fdb.kv_get("fan_speed") then
                    speed = fan_sp
                end
                del_fanspeed()
                bt_add = 0

            end
        else 
            if button_cont < button_shake_time then
            else
                if button_cont < button_long_time then
                    print("pass")
                     
                    add_fanspeed()

                    bt_add = 0
                elseif (button_cont > button_long_time) and  (button_cont < button_wait_time)  then
                    bt_add = bt_add + 3
                    if bt_add%2 == 1 then
                   
                        speed = 0


                        bt_add = 3
                    elseif bt_add%2 == 0 then

                        get_fanspeed()
                        bt_add = 0
                    end

                end
            end
           
            button_cont = 0
            button_state = false
            button_detect = true
        end
    end
end,button_timer_outtime) 

sys.taskInit(function()
    
    
log.info("lvgl", lvgl.init()) --初始化 lvgl
if lvgl.theme_set_act then
    -- 切换主题
    -- lvgl.theme_set_act("default")
   --  lvgl.theme_set_act("mono")
   --lvgl.theme_set_act("empty")
    -- lvgl.theme_set_act("material_light")
    lvgl.theme_set_act("material_dark")
     --lvgl.theme_set_act("material_no_transition")
    -- lvgl.theme_set_act("material_no_focus")
end

--lvgl.disp_set_bg_color(nil, 0x000000) --设置 lcd 背景色



 scr = lvgl.obj_create(nil, nil) --创建屏幕
--[[ btn = lvgl.btn_create(scr)
lvgl.obj_set_size(btn,130,18)
lvgl.obj_align(btn, lvgl.scr_act(), lvgl.ALIGN_CENTER,demo_x,demo_y)
 label_demo = lvgl.label_create(btn) --创建按钮文字
lvgl.label_set_recolor(label_demo,true)
lvgl.label_set_text(label_demo, "#00ff00 Air105-MP3 #") ]]--创建按钮文字，此处根据按键显示不同的模式

label_demo =lvgl.label_create(scr)
--lvgl.obj_set_size(label_time,120,25)
lvgl.obj_set_pos(label_demo,10,20);
--lvgl.obj_set_pos(label_time, 0, 12);
--lvgl.obj_align(label_time, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 20)
lvgl.label_set_recolor(label_demo,true)
lvgl.label_set_text(label_demo, "#00ff00 Air105 #")    

font = lvgl.font_get("opposans_m_12")


lvgl.scr_load(scr)
lvgl.obj_set_style_local_text_font(lvgl.scr_act(), lvgl.OBJ_PART_MAIN, lvgl.STATE_DEFAULT, font)



    pwm.open(device.pwm_ID, 40*1000, 0)
	--pwm.open(2, 1000, 1)
--	rotary_start()
    while true do
       
       
      --[[  speed = speed + 10
        if speed > 100 then speed = 0 end
        log.info("pwm","speed now",speed,"%")]]
		--log.info("pwm","speed now",speed,"%")
        pwm.open(device.pwm_ID, 40*1000, speed)
        lvgl.label_set_text(label_demo, "#00ff00 风速:"..speed.."% #")  
		--pwm.open(2, 1000, speed)
        sys.wait(100)
  
    end
end)

sys.run()
-- sys.run()之后后面不要加任何语句!!!!!